//
//  PrescriptionOrderCellViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/8/9.
//  Copyright © 2021 QDSG. All rights reserved.
//

import UIKit

class PrescriptionOrderCellViewModel: NSObject {
    let model: PrescriptionOrder
    
    init(model: PrescriptionOrder) {
        self.model = model
    }
    
    var url: String { model.url ?? "" }
    var doctorName: String { model.doctorName ?? "" }
    var departmentName: String { model.deptName ?? "" }
    
    /// 快递单号
    var expressNum: String { model.expressNum ?? "" }
    
    var receiveType: PrescriptionOrderReceiveType { model.receiveType ?? .unknown }
    
    /// 剩余支付时间
    var remainPayTime: NSAttributedString {
        if model.remainPayTime == "0" {
            let str = "已超时"
            let attributes = [NSAttributedString.Key.foregroundColor: UIColor(hex: "#F54C4C")]
            let attrStr = NSMutableAttributedString(string: str)
            attrStr.setAttributes(attributes, range: NSRange(location: 0, length: str.count))
            return attrStr
        }

        let tempStr = Date.timeStampToString(timeStamp: (model.remainPayTime ?? "0").intValue)
        let str = "请在\(tempStr)内完成支付"
        let attributes = [NSAttributedString.Key.foregroundColor: UIColor(hex: "#F54C4C")]
        let attrStr = NSMutableAttributedString(string: str)
        attrStr.setAttributes(attributes, range: NSRange(location: 2, length: tempStr.count))
        return attrStr
    }
    var orderId: String { model.orderId ?? "" }
    var prescriptionId: String { model.prescriptionId ?? "" }
    /// 处方药名
    var drugsName: String { model.prescriptionList ?? "" }
    /// 处方总金额
    var totalCost: String { "￥\(model.prescriptionAmount ?? "")" }

    var status: PrescriptionOrderStatus? { model.status }
    
    var statusName: String { model.statusName ?? "" }

    /// 已超时
    var payTimeout: Bool { model.remainPayTime == "0" }
    
    var cellHeight: CGFloat {
        guard let status = status else { return 0.0 }
        switch status {
        case .canceled,
             .reject,
             .refunded:  return 166.0
        case .completed: return receiveType == .express ? 216.0 : 166.0
        default:         return 216.0
        }
    }
    
    var leftIsHidden: Bool {
        guard let status = status else { return true }
        
        switch status {
        case .waitPay,
             .waitShip,
             .shipped,
             .waitTake,
             .waitSendOut,
             .waitSendOutUnreceived,
             .waitSendOutReceived:  return false
        case .completed:    return receiveType == .express ? false : true
        default:            return true
        }
    }
    
    var bottomHidden: Bool {
        guard let status = status else { return true }
        
        switch status {
        case .reject,
             .canceled,
             .refunded:  return true
        case .completed: return receiveType == .express ? false : true
        default:         return false
        }
    }
}
